#include "main.h"
#include "radio.h"
uint32_t g_year   = YEAR;
uint32_t g_month  = MONTH+1;
uint32_t g_day    = DAY;
uint32_t g_hour   = HOUR;
uint32_t g_minute = MINUTE;
uint32_t g_second = SECOND;

/*
状态指示灯：在boot程序中运行，状态指示灯每100ms闪烁一次，快闪；
	       在APP程序中运行，每500ms闪烁一次；
		   在APP程序运行初始30S，快闪，然后每500ms闪烁一次，主要用于测试
*/
uint8_t gStatusLEDFlickerTimeoutSn = 0;
uint32_t ul_radio_status_led_timeout_cnt = TIME_OUT_500MS;
/*
错误指示灯：CAN接收通信错误，长亮，同时CAN通信指示灯快速闪烁；
           CAN发送通信错误，不亮，同时CAN通信指示灯快速闪烁
*/


/*
通信指示灯：CAN正常收发，指示灯每250ms闪烁一次；
           CAN收发错误，指示灯每100ms闪烁一次，快闪；
*/
uint8_t gCanTxFaultSn = 0;
uint8_t gCanRxFaultSn = 0;
uint32_t gCanRxFaultCnt = 0;/*CAN 接收错误计数*/

uint8_t gSpeedLEDFlickerTimeoutSn = 0;
uint32_t gSpeedLedTimeoutCnt = TIME_OUT_250MS;
uint32_t gCuur = 0;
void led_tick(void)
{
	if(ul_system_tick%gSpeedLedTimeoutCnt == 0)
	{
		if(gSpeedLEDFlickerTimeoutSn == 1)
		{
			gSpeedLEDFlickerTimeoutSn = 0;
		}		
		else
		{
			gSpeedLEDFlickerTimeoutSn = 1;
		}
	}

	if (ul_system_tick % TIME_OUT_100MS == 0)
	{
		if(gCanRxCnt != gCanRxCntLast)
		{
			gCanRxCntLast = gCanRxCnt;
		}
		else
		{
			gCanRxFaultCnt++;
		}
    }
}
void tick_Callback(void)
{
    g_out_timeout_cntr++;
	if(ul_system_tick % 200 == 0)
	{
		uc_can_tx_sn = 1;
	}
    if (ul_system_tick % 2 == 0)
	{
		output_update();
		App_tick_update();
	}
	else
	{
		input_update();
        Can_Tx_Fifo();
	}
    led_tick();
}
uint32_t ul_system_tick = 0;
uint8_t uc_feedDog_sn = 0;
void Main_Tick(void)
{
	ul_system_tick++;
	radio_tick(ul_system_tick);
    if(ul_system_tick%50 == 0)
    {
        uc_feedDog_sn = 1;			//这个三行代码不能动，不然会造成无法喂狗，CPU会反复复位，后果是变成砖头！！！
    }
    
	tick_Callback();
}
void Main_LedCtrl(void)
{
	//CAN 接收错误判断,未收到报文
	if(gCanRxFaultCnt > 10) gCanRxFaultSn = 1;
	else gCanRxFaultSn = 0;

	//CAN通信指示灯
	if(gSpeedLEDFlickerTimeoutSn == 1) IO_LedCtrl(xSPEED_LED,LED_ON);
	else IO_LedCtrl(xSPEED_LED,LED_OFF);
	
	if((gCanRxFaultSn == 1) || (gCanTxFaultSn == 1))
	{
		gSpeedLedTimeoutCnt = TIME_OUT_100MS;
	}
	else
	{
		gSpeedLedTimeoutCnt = TIME_OUT_250MS;
	}
	//错误指示灯
	if(gCanRxFaultSn == 1) IO_LedCtrl(FAULT_LED,LED_ON);
	else IO_LedCtrl(FAULT_LED,LED_OFF);
}
/**
 * @brief 参数初始化
 */
void Main_Sw_Init(void)
{
	uint32_t i = 0;

	hem_config_t default_config;
	memset(&default_config,0x00,sizeof(default_config));
	
	default_config.baudrate = 250;		//波特率

	/****************滚扫配置*****************/
	//滚扫X00~X07端口配置，其中X00和X01组成一路全桥，X02和X03组成一路全桥
	default_config.port_mode[0] = eHighSideOut;//X00
	default_config.port_mode[1] = eHighSideOut;//X01
	default_config.port_mode[2] = eHighSideOut;//X02
	default_config.port_mode[3] = eHighSideOut;//X03
	default_config.port_mode[4] = eHighSideOut;//X04
	default_config.port_mode[5] = eHighSideOut;//X05
	default_config.port_mode[6] = eInput;	   //X06-PI,DI
	
	default_config.port_mode[7] = eHighSideOut;//X07
	//X00~X07输入配置，配置能够采样的电压范围，当端口为输出时，配置成36V采样
	default_config.in_mode[0] = IN_MODE_36V;//DI压力继电器
	default_config.in_mode[1] = IN_MODE_36V;//DO
	default_config.in_mode[2] = IN_MODE_36V;
	default_config.in_mode[3] = IN_MODE_36V;
	default_config.in_mode[4] = IN_MODE_36V;
	default_config.in_mode[5] = IN_MODE_36V;
	default_config.in_mode[6] = IN_MODE_36V;
	default_config.in_mode[7] = IN_MODE_36V;
	
	//IN08和IN09输入配置
	default_config.in_mode[8] = IN_MODE_36V;//温度传感器
	default_config.in_mode[9] = IN_MODE_36V;

	for(i = 0;i < HEM_PWM_FREQ_NUM;i++)
	{
		default_config.pwm_freq[i] = 200;
	}

	default_config.pwmi_kp100 = 112;
	default_config.pwmi_ki100 = 48;	
	default_config.pwmi_kd100 = 16;	

	node_config_init(&default_config);
	set_baudrate = g_config.baudrate;
}
void Main_FeedDog(void)
{
	if (uc_feedDog_sn == 0) return;
	uc_feedDog_sn = 0;

	WDT_Feed();
}
void Main_read_version(void)
{
	uint8_t buf[8] = { 0 };
	if((receive_message_bootloader.rx_data[0] == 0xF0) &&  //命令
	   (receive_message_bootloader.rx_data[1] == 0x56) &&
	   (receive_message_bootloader.rx_data[2] == 0x45) &&
	   (receive_message_bootloader.rx_data[3] == 0x52) &&
	   (receive_message_bootloader.rx_data[4] == 0x53) &&
	   (receive_message_bootloader.rx_data[5] == 0x49) &&
	   (receive_message_bootloader.rx_data[6] == 0x4F) &&
	   (receive_message_bootloader.rx_data[7] == 0x4E)
	  )
	{
		buf[0] = 0xF0;
		buf[1] = (uint8_t)((g_year>>8)&0xFF);
		buf[2] = (uint8_t)((g_year>>0)&0xFF);
		buf[3] = (uint8_t)((g_month>>0)&0xFF);
		buf[4] = (uint8_t)((g_day>>0)&0xFF);
		buf[5] = (uint8_t)((g_hour>>0)&0xFF);
		buf[6] = (uint8_t)((g_minute>>0)&0xFF);
		buf[7] = (uint8_t)((g_second>>0)&0xFF);
		Can_Data_Store_Fifo(ACK_TX_ID,0,buf,8);
	}
}
void Main_HW_Init(void)
{
	SCB->VTOR = FLASH_BASE | APP_LOAD_ADDR_OFFSET;
	systick_config();

	IO_Init();
	CAN_init();
	ADC_Init();
	PWM_Init(g_config.pwm_freq[0], g_config.pwm_freq[1], g_config.pwm_freq[2], g_config.pwm_freq[3]);
#if X06_IS_PI
	PI_Init();
#endif
    app_input_init();
	WDT_Init();
	USART_init(9600);

	IO_LedCtrl(xSPEED_LED,LED_OFF);
	IO_LedCtrl(STATUS_LED,LED_OFF);
	IO_LedCtrl(FAULT_LED,LED_OFF);
}

int main(void)
{
	Main_Sw_Init();
	Main_HW_Init();
	while (1)
	{
    	app_input_init();
		Main_LedCtrl();
		system_Reset();
		Main_FeedDog();
        Main_read_version();
		App_run();
		redio_run();
	}
}


